﻿using System;
using System.Web;
using RedHouse.Data.Common;
using RedHouse.Data.Base;
using RedHouse.Data;

namespace RedHouse.Web.Handler
{
    public class EmailHandler : IHttpHandler
    {
        /// <summary>
        /// You will need to configure this handler in the web.config file of your 
        /// web and register it with IIS before being able to use it. For more information
        /// see the following link: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpHandler Members
        private string _successMessageTemplate = "<script>alert('Email confirmed! Thanks for your subcription!')</script>" ;
        private string _errorMessageTemplate = "<script>alert('Error while process your request!')</script>";
        public bool IsReusable
        {
            // Return false in case your Managed Handler cannot be reused for another request.
            // Usually this would be false in case you have some state information preserved per request.
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            HttpRequest rq = context.Request;
            HttpResponse rs = context.Response;
            string emailAddress = rq["email"];
            Subcribe subcribe = new Subcribe()
            {
                Email = emailAddress
            };
            OperationResult confirmResult = SubcribeService.Instance.ActiveSubcribe(subcribe);
            if (confirmResult.Result)
            {
                rs.Write(_successMessageTemplate);
            }
            else
            {
                rs.Write(_errorMessageTemplate);
            }

            rs.Redirect("~/Home.aspx");

        }

        #endregion
    }
}
